import { _decorator, Component, Node, Quat, SkeletalAnimation, Vec3 } from 'cc';
import { aa } from '../frame/FrameCore';
import { anmClip, anmType } from '../frame/common/Enums';
const { ccclass, property } = _decorator;

const eyeLend: any[] = [new Vec3(1, -102, 15), new Vec3(-5, -90, 0)]
const eyeRend: any[] = [new Vec3(35, -85, 3), new Vec3(-5, -90, 0)]
let eyeLstart = []
let eyeRstart = []
let mouthStart: any[] = [new Vec3(10, -90, 0), new Vec3(12, -90, 0)];
const mouthEnd: any[] = [new Vec3(7.4, -90, 0), new Vec3(19, -90, 0)];
let _tempQ = new Quat()

@ccclass('PlayerCtrl')
export class PlayerCtrl extends Component {
    @property(SkeletalAnimation)
    anim: SkeletalAnimation = null;
    @property(Node)
    eyeL: Node[] = [];
    @property(Node)
    eyeR: Node[] = [];
    @property(Node)
    mouth: Node[] = [];
    @property(Node)
    headNub: Node = null;
    private _t = 0;
    private _d = 1;
    isPause = true
    pauseTime = 2.8;
    blinkL = 1;
    blinkR = 1;
    _s = 0.6;
    _m0 = 0.1;
    _m1 = 0.8;
    _u = 1;
    _up = 0.0;
    _bottom = 0.4;
    start() {
        eyeLend[0] = Quat.fromEuler(new Quat(), eyeLend[0].x, eyeLend[0].y, eyeLend[0].z);
        eyeLend[1] = Quat.fromEuler(new Quat(), eyeLend[1].x, eyeLend[1].y, eyeLend[1].z);
        eyeRend[0] = Quat.fromEuler(new Quat(), eyeRend[0].x, eyeRend[0].y, eyeRend[0].z);
        eyeRend[1] = Quat.fromEuler(new Quat(), eyeRend[1].x, eyeRend[1].y, eyeRend[1].z);
        eyeLstart = [this.eyeL[0].rotation.clone(), this.eyeL[1].rotation.clone()];
        eyeRstart = [this.eyeR[0].rotation.clone(), this.eyeR[1].rotation.clone()];
        // mouthStart = [this.mouth[0].rotation.clone(), this.mouth[1].rotation.clone()];
        mouthStart[0] = Quat.fromEuler(new Quat(), mouthStart[0].x, mouthStart[0].y, mouthStart[0].z);
        mouthStart[1] = Quat.fromEuler(new Quat(), mouthStart[1].x, mouthStart[1].y, mouthStart[1].z);
        mouthEnd[0] = Quat.fromEuler(new Quat(), mouthEnd[0].x, mouthEnd[0].y, mouthEnd[0].z);
        mouthEnd[1] = Quat.fromEuler(new Quat(), mouthEnd[1].x, mouthEnd[1].y, mouthEnd[1].z);
        aa.global.isTalk = true;
    }


    play(anmClip:anmType){
        this.anim.crossFade(anmClip.name,anmClip.crossfade);
    }

    victory(){
        this.play(anmClip.victory);
        this.scheduleOnce(()=>{
            this.play(anmClip.idle);
        },2.4)
    }

 

    setMouth(value){
        this._s = this._m1*0.9;
    }

    lateUpdate(dt: number) {
        if (aa.global.isGame) return;
        if (aa.global.isTalk){
            if (this._s >= this._m1) {
                this._s = this._m1;
                this._u = -1
                this._m0 = 0.4 * Math.random();
            }
            if (this._s <= this._m0) {
                this._s = this._m0;
                this._u = 1
                this._m1 = 0.7 + 0.3 * Math.random();
            }
            this._s += this._u * dt * 2.0;
            this.mouth[0].rotation = Quat.slerp(_tempQ, mouthStart[0], mouthEnd[0], this._s);
            this.mouth[1].rotation = Quat.slerp(_tempQ, mouthStart[1], mouthEnd[1], this._s);
        }else{
            this.mouth[0].rotation = Quat.lerp(_tempQ, mouthStart[0], mouthEnd[0], this._up);
            this.mouth[1].rotation = Quat.lerp(_tempQ, mouthStart[1], mouthEnd[1], this._bottom);
        }
   
 
        if (this.isPause) {
            this.pauseTime -= dt;
            if (this.pauseTime <= 0) {
                this.pauseTime = 2.5 + Math.random() * 2;
                this.isPause = false;
            }
        }
        if (this.isPause) return;
        this._t += dt * this._d * 10;
        if (this._t >= 1) {
            this._d = -1.5;
            this._t = 1;
        }
        if (this._t <= 0) {
            this._t = 0;
            let blinked = Math.random();
            if (blinked < 0.7) {
                this.blinkL = 1;
                this.blinkR = 1;
                this._d = 1;
            } else if (blinked < 0.85) {
                this.blinkL = 1;
                this.blinkR = 0;
                this._d = 0.85;

            } else {
                this.blinkL = 0;
                this.blinkR = 1;
                this._d = 0.85;

            }
            this.isPause = true;
        }
        const r = this._t * this.blinkR;
        const l = this._t * this.blinkL;

        this.eyeL[0].rotation = Quat.slerp(_tempQ, eyeLstart[0], eyeLend[0], r);
        this.eyeL[1].rotation = Quat.slerp(_tempQ, eyeLstart[1], eyeLend[1], r);
        this.eyeR[0].rotation = Quat.lerp(_tempQ, eyeRstart[0], eyeRend[0], l);
        this.eyeR[1].rotation = Quat.lerp(_tempQ, eyeRstart[1], eyeRend[1], l);

    }
}

